function getSum() {
    var check;
    var sum = 0;
    var num = 0;
    $(".shoppingCarInfo_isselect").each(
        function () {
            check = $(this).attr("selectit");
            if(check=='true')
            {
                var price = $(this).parent().next().next().next().next().children().text().substring(1).replace(/,/g,"");
                price = Number(price);
                // console.log(price);
               sum+=price;
               var number = parseInt($(this).parent().next().next().next().children("input").val());
               num+=number;
            }
            // console.log(check);
        }
    );
    $(".myShoppingSum").text("￥"+fmoney(sum,3));
    $(".myShoppingNum").text(num);
}
function checkAll() {
    var check;
    var num = 0;
    var len = $(".shoppingCarInfo_isselect").length;
    $(".shoppingCarInfo_isselect").each(
        function () {
            check = $(this).attr("selectit");
            if(check=='false')
            {
                // console.log(check);
                num++;
            }
            // console.log(check);

        }
    );
        if(num>0||len==0)
    {
        $(".selectAllItem").attr("src","img/site/cartNotSelected.png");
        $(".selectAllItem").attr("selectit","false");
        // console.log($(".selectAllItem").attr("selectit"));
        if(num == len)
        {
            $(".myShoppingButton").css("background-color","#aaa");
            $(".myShoppingButton").attr("disabled","false");
        }

    }
     else if(num==0)
    {
        $(".selectAllItem").attr("src","img/site/cartSelected.png");
        $(".selectAllItem").attr("selectit","true");

    }
    // console.log(check);


};
function fmoney(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
};
function compute(cla,account,number)
{
    var sum = $(cla).parent().next().children();
    sum.text("￥"+fmoney(account*number,3));
}
function getAccount(cla)
{
    var text = $(cla).parent().prev().find(".shoppingCarInfo_nowPrice").text();
    // console.log(text);
    var account = parseFloat(text.substring(1));
    return account;
}
var deleteOrderItem = false;
var deleteOrderItemid = 0;
$(function () {

    $(".NumberUp").click(
        function () {
            var account = getAccount(this);
            var num = parseInt($(this).next().val());
            num++;
            var number = num;
            $(this).next().val(num);
            compute(this,account,number);
            // console.log( $(this).next().val());
            getSum();
            var url = "foreupdateCart";
            var oiid = $(this).parent().parent().attr("oiid");
            $.post(url,{
                "oiid":oiid,
                "num":number
            },
                function (data) {
                    if(data=="fail")
                    {
                        location.href="login.jsp";
                    }
                    else
                    {

                    }
                });
        }
    );
    $(".NumberDown").click(
        function () {
            var account = getAccount(this);
            var num = parseInt($(this).prev().val());
            num--;
            if(num<0)
                num=0;
            $(this).prev().val(num);
            compute(this,account,num);
            // console.log( $(this).prev().val());
            getSum();
            var url = "foreupdateCart";
            var oiid = $(this).parent().parent().attr("oiid");
            $.post(url,{
                    "oiid":oiid,
                    "num":number
                },
                function (data) {
                    if(data=="fail")
                    {
                        location.href="login.jsp";
                    }
                    else
                    {

                    }
                });
        }
    );
    $(".number_num").keyup(
        function () {
            var num =  parseInt($(this).val());
            if (isNaN(num))
                num = 1;
            if(num<=0)
                num = 1;
            $(this).val(num);
            var account = getAccount(this);
            compute(this,account,num);
            getSum();
    });
    $(".shoppingCarInfo_isselect").click(
        function () {
            var isSelected = $(this).attr("selectit");
            // console.log( isSelected );
            if(isSelected=='false')
            {

                // console.log("1");
                $(this).attr("src","img/site/cartSelected.png");
                $(this).attr("selectit","true");
                $(this).parent().parent().css("background-color","rgb(255, 248, 225)");
                $(".myShoppingButton").css("background-color","#C40000");
                $(".myShoppingButton").removeAttr("disabled");
            }
            else
            {
                // console.log("0");
                $(this).attr("src","img/site/cartNotSelected.png");
                $(this).attr("selectit","false");
                $(this).parent().parent().css("background-color","white");


            }
            checkAll();
            getSum();
        }
    ) ;
    $(".selectAllItem").click(
        function () {
            var isSelected = $(this).attr("selectit");
            // console.log( isSelected );
            if(isSelected=='false')
            {
                $(".shoppingCarInfo_isselect").each(
                    function () {
                        $(this).attr("src","img/site/cartSelected.png");
                        $(this).attr("selectit","true");
                        $(this).parent().parent().css("background-color","rgb(255, 248, 225)");
                    }
                );
                $(".selectAllItem").attr("src","img/site/cartSelected.png");
                $(".selectAllItem").attr("selectit","true");
                $(".myShoppingButton").css("background-color","#C40000");
                $(".myShoppingButton").removeAttr("disabled");
            }
            else
            {
                $(".shoppingCarInfo_isselect").each(
                    function () {
                        $(this).attr("src","img/site/cartNotSelected.png");
                        $(this).attr("selectit","false");
                        $(this).parent().parent().css("background-color","white");
                    }
                );
                $(".selectAllItem").attr("src","img/site/cartNotSelected.png");
                $(".selectAllItem").attr("selectit","false");
            }
            checkAll();
            getSum();
        }
    );
    $(".shoppingCarInfo_delete").click(
        function () {
            // console.log($(this).parents("table").get(0))
            $(this).parent().remove();
            // console.log($(".shoppingCarInfo_isselect").length);
            checkAll();
            getSum();
        }
    );
    var sels = new Array();
    $(".myShoppingButton").click(
        function () {
            $(".shoppingCarInfo_isselect").each(
                function () {
                    var select = $(this).attr("selectit");
                    sels.push(select);
                }
            );
            console.log(sels);
            var sum = $($(".myShoppingSum")[0]).text();
            sum =sum.substring(1);
            var url = "foreorder";
            var mysels = {
                "sels":sels
            };
            $.post(
                url,
                {
                    "sum":sum,
                    "data":JSON.stringify(mysels)
                },
                function () {
                    location.href="order.jsp";
        }
            );
        }
    );

    $(".deleteLink").click(
        function () {
            deleteOrderItem = false;
            deleteOrderItemid = $(this).attr("oiid");
            $("#deleteModal").modal('show');
            return false;
        }
    );
    $("#deleteConfirm").click(
        function () {
            deleteOrderItem = true;
            $("#deleteModal").modal('hide');
        }
    );
    $("#deleteModal").on('hidden.bs.modal',
        function (e) {
            if(deleteOrderItem)
            {
                var url = "foredeleteCart";
                $.post(
                    url,
                    {
                        "oiid":deleteOrderItemid
                    },
                    function (data) {
                        if("success"==data){
                           location.reload();
                        }
                        else{
                            location.href="login.jsp";
                        }
                    }

                );
            }
        });
});